/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kite;

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author ADMIN
 */
public class LinkParser
{

    public static void getHaiVL() throws IOException
    {        
        String url = "http://www.haivl.com/new/552";
        print("Fetching %s...", url);

        Document doc = Jsoup.connect(url).get();        
        Elements media = doc.select("[src]");        

        print("\nMedia: (%d)", media.size());
        for (Element src : media)
        {
            if (src.tagName().equals("img"))
            {
                String s = src.getElementsByTag("img").attr("src");
                if (s.startsWith("/data/photos"))
                {
                    System.out.println("tag : " + "http://www.haivl.com" + s);
                }
            }
        }
    }
    
    public static void getEpic() throws IOException{
         String url = "http://m.epic.vn/moi?page=2";
        print("Fetching %s...", url);

        Document doc = Jsoup.connect(url).get();        
        Elements media = doc.select("[src]");        

        print("\nMedia: (%d)", media.size());
        for (Element src : media)
        {
            if (src.tagName().equals("img"))
            {
                String s = src.getElementsByTag("img").attr("src");
         //       if (s.startsWith("/data/photos"))
                {
                    System.out.println("tag : " + s);
                }
            }
        }
    }
    
    public static void main(String[] args) throws IOException{
        getHaiVL();
    }
            

    /**
     * @param args the command line arguments
     */
    public static void main1(String[] args) throws IOException
    {

        //Validate.isTrue(args.length == 1, "usage: supply url to fetch");
        String url = "http://www.haivl.com/new/553";
        print("Fetching %s...", url);

        Document doc = Jsoup.connect(url).get();
        Elements links = doc.select("a[href]");
        Elements media = doc.select("[src]");
        Elements imports = doc.select("link[href]");

        print("\nMedia: (%d)", media.size());
        for (Element src : media)
        {
            if (src.tagName().equals("img"))
            {
                String s = src.getElementsByTag("img").attr("src");
                if (s.startsWith("/data/photos"))
                {
                    System.out.println("tag : " + "http://www.haivl.com" + s);
                }
                print(" * %s: <%s> %sx%s (%s)",
                        src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
                        trim(src.attr("alt"), 20));
            } else
            {
                print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
            }
        }

        print("\nImports: (%d)", imports.size());
        for (Element link : imports)
        {
            print(" * %s <%s> (%s)", link.tagName(), link.attr("abs:href"), link.attr("rel"));
        }

        print("\nLinks: (%d)", links.size());
        for (Element link : links)
        {
            print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
        }
    }

    private static void print(String msg, Object... args)
    {
        System.out.println(String.format(msg, args));
    }

    private static String trim(String s, int width)
    {
        if (s.length() > width)
        {
            return s.substring(0, width - 1) + ".";
        } else
        {
            return s;
        }
    }
}
